package com.apache.order.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.order.entity.TOrderDetail;
import com.apache.order.manager.TOrderDetailManager;

/**
 * description:  订单详细信息
 * @author Hou Dayu 创建时间：2015-8-3  
 */
public class TOrderDetailManagerImpl implements TOrderDetailManager {

	protected IDao tOrderDetailDao;
	protected final String entityName = "com.apache.order.entity.TOrderDetail";

	public void settOrderDetailDao(IDao tOrderDetailDao) {
		this.tOrderDetailDao = tOrderDetailDao;
	}

	public String saveInfo(ParamsVo<TOrderDetail> vo) throws BusinessException {
		TOrderDetail tOrderProduct = vo.getObj();
		if (Validator.isEmpty(tOrderProduct)) {
			List<TOrderDetail> list = vo.getObjs();
			MethodParam param = new MethodParam("Batch", "", "", entityName);
			param.setVaule(list);
			if (tOrderDetailDao.insert(param)) {
				return "true";
			}
		} else {
			String infoId = Validator.generate();
			//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
			String cacheKey = "";
			tOrderProduct.setDetailedId(infoId);
			MethodParam param = new MethodParam("TOrderDetail", cacheKey, "", entityName);
			param.setVaule(tOrderProduct);
			if (tOrderDetailDao.insert(param)) {
				return infoId;
			}
		}
		return "";
	}

	public boolean editInfo(ParamsVo<TOrderDetail> vo) throws BusinessException {
		TOrderDetail tOrderProduct = vo.getObj();
		if (Validator.isNotNull(tOrderProduct.getDetailedId())) {
			String key = Validator.getDefaultStr(vo.getMethodKey(), "TOrderProduct");
			String cacheKey = "";
			//String cacheKey="tOrderProduct_"+tOrderProduct.getOrderDetailedId();
			MethodParam param = new MethodParam(key, cacheKey, "", entityName);
			param.setVaule(tOrderProduct);
			return tOrderDetailDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<TOrderDetail> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="tOrderProduct_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
		if ("ById".equals(key)) {
			param.setInfoId(infoId);
			TOrderDetail info = (TOrderDetail) tOrderDetailDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			param.setVaule(info);//此处需要先将状态值赋值为删除状态
		}
		param.setKey(key);
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("TOrderProduct");
			return tOrderDetailDao.edit(param);
		} else {
			if ("ById".equals(key)) {
				param.setParams("orderDetailedId", infoId);
			} else {
				param.setParams("orderId", infoId);
			}
			param.setDelete(true);
			return tOrderDetailDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<TOrderDetail> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="tOrderProduct_"+infoId;
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		if (Validator.isNull(infoId)) {
			TOrderDetail product = vo.getObj();
			if (null == product)
				return null;
			param = new MethodParam("ByOrderIdAndProdId", cacheKey, "", entityName);
			param.setParams("orderId", product.getOrderId());
			param.setParams("productId", product.getProductId());
			return tOrderDetailDao.selectSingle(param);
		}
		param.setInfoId(infoId);
		return tOrderDetailDao.selectById(param);
	}

	public Object execute(ParamsVo<TOrderDetail> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<TOrderDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = tOrderDetailDao.pageSelect(param);
		return page;
	}

	public List<TOrderDetail> getList(ParamsVo<TOrderDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return tOrderDetailDao.select(param);
	}

	public long countInfo(ParamsVo<TOrderDetail> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return tOrderDetailDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<TOrderDetail> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(tOrderDetailDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		TOrderDetail tOrderProduct = vo.getObj();
		if (!Validator.isEmpty(tOrderProduct)) {
			if (Validator.isNotNull(tOrderProduct.getOrderId())) {
				param.setParams("orderId", tOrderProduct.getOrderId());
			}
			if (Validator.isNotNull(tOrderProduct.getProductId())) {
				param.setParams("productId", tOrderProduct.getProductId());
			}
			if (Validator.isNotNull(tOrderProduct.getProductName())) {
				param.setParams("productName", "'%" + tOrderProduct.getProductName() + "%'");
			}
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
